package com.swh.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPoolConfig;


/**
 * @Author: swh
 * @Description:
 * @Date: 2020/12/25 10:30
 * @Version: 1.0
 */
@Configuration
@EnableCaching
@PropertySource(value = "classpath:reids.properties")
public class ReidsConfig {
    @Value("${spring.redis.host}")
    public String host;
    @Value("${spring.redis.password}")
    public String password;
    @Value("${spring.redis.port}")
    public int port;

    @Value("${spring.redis.Database}")
    public int database;

    @Value("${spring.redis.pool.max-idle}")
    public int maxIdle;
    @Value("${spring.redis.pool.max-active}")
    public int maxActive;

    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxActive);
        return config;
    }

    @Bean
    public RedisConnectionFactory  getJedisConnectionFactory(JedisPoolConfig config) {
        //旧版本配置
//        JedisConnectionFactory factory = new JedisConnectionFactory();
//        factory.setHostName(host);
//        factory.setDatabase(database);
//        factory.setPort(port);
//        factory.setPassword(password);
//        factory.setPoolConfig(config);
//        return factory;

        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPort(port);
        configuration.setPassword(password);
        configuration.setDatabase(database);
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder =
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        builder.poolConfig(config);
        return new JedisConnectionFactory(configuration, builder.build());
    }

    public RedisConnectionFactory getConnectionFactory(JedisPoolConfig config) {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPassword(password);
        configuration.setPort(port);
        configuration.setDatabase(database);

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        builder.poolConfig(config);

        return new JedisConnectionFactory(configuration, builder.build());

    }

    //旧版本
//    @Bean
//    public RedisTemplate getRedisTemplate(JedisConnectionFactory factory) {
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(factory);
//
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        GenericJackson2JsonRedisSerializer genredisSerializer = new GenericJackson2JsonRedisSerializer();
////        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
//
//
//        template.setKeySerializer(stringRedisSerializer);
//        template.setValueSerializer(genredisSerializer);
//
//        template.setHashKeySerializer(stringRedisSerializer);
//        template.setHashValueSerializer(genredisSerializer);
//
//        //开启事务setEnableTransactionSupport
//        template.setEnableTransactionSupport(true);
//        template.setEnableDefaultSerializer(true);
//        template.setDefaultSerializer(genredisSerializer);
//
//        return template;
//
//    }

    //旧版本
//    @Bean
//    public RedisCacheManager getCacheManager(RedisTemplate redisTemplate){
//        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//        Collection<String> collection = new ArrayList<>();
//        collection.add(Office.SYS_OFFICE_KEY);
//        cacheManager.setCacheNames(collection);
//        return cacheManager;
//    }




}
